package com.flag.liutao.modules.student.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flag.liutao.common.handler.MyException;
import com.flag.liutao.modules.student.domain.Classes;
import com.flag.liutao.modules.student.domain.Major;
import com.flag.liutao.modules.student.mapper.ClassesMapper;
import com.flag.liutao.modules.student.param.ClassParam;
import com.flag.liutao.modules.student.service.IClassesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flag.liutao.modules.student.vo.ClassVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author godM
 * @since 2022-12-06
 */
@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements IClassesService {
    @Autowired
    private ClassesMapper classesMapper;


    @Override
    public Map<String, Object> getPage(int page, int limit, String keyword, Long majorId, Long gradeId) {
        IPage<ClassVo> iPage  = new Page<>(page,limit);
        IPage<ClassVo> pageDB = classesMapper.getClassesPage(iPage,keyword,majorId,gradeId);
        Map<String,Object> map = new HashMap<>();
        map.put("content",pageDB.getRecords());
        map.put("total",pageDB.getTotal());
        return map;
    }

    @Override
    public void addClass(ClassParam param) {
        LambdaQueryWrapper<Classes> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Classes::getClassName,param.getClassName());
        Classes classes = classesMapper.selectOne(lqw);
        if(ObjectUtil.isNotNull(classes)){
            throw new MyException("班级名称"+param.getClassName()+"已被使用");
        }
        Classes classDB = new Classes();
        BeanUtil.copyProperties(param,classDB);
        classesMapper.insert(classDB);
    }

    @Override
    public void editClass(ClassParam param) {
        Classes classes1 = classesMapper.selectById(param.getId());
        if (ObjectUtil.isNull(classes1)) {
            throw new MyException("该班级不存在");
        }
        LambdaQueryWrapper<Classes> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Classes::getClassName,param.getClassName());
        Classes classes2 = classesMapper.selectOne(lqw);
        if(ObjectUtil.isNotNull(classes2) && classes2.getId()!=param.getId()){
            throw new MyException("班级名称："+param.getClassName()+"已被使用");
        }
        Classes classes = new Classes();
        BeanUtil.copyProperties(param,classes);
        classesMapper.updateById(classes);
    }

    @Override
    public void delClass(Long id) {
        classesMapper.deleteById(id);
    }
}
